package cn.nawang.ebeim.client.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;

import com.esotericsoftware.minlog.Log;

import cn.nawang.ebeim.client.bean.ClientConfiguration;

public class FileUtil {

  public static final String PATH_VERSION = "version";
  public static final String PATH_ZIP = "zip";
  public static final String PATH_UPLOAD = "upload";
  public static final String PATH_DOWNLOAD = "download";
  public static final String PATH_DATABASE = "database";
  public static final String PATH_WORKING = "working";
  public static final String PATH_BACKUP_LOCAL = "local";

  public static String getDbUploadPath(String dsId, String dbName) {
    return ClientConfiguration.getWorkingPath() + File.separator + dsId + File.separator
        + PATH_DATABASE + File.separator + PATH_UPLOAD + File.separator + dbName;
  }
  
  public static String getDbUploadPath(String dsId) {
      return ClientConfiguration.getWorkingPath() + File.separator + dsId + File.separator
          + PATH_DATABASE + File.separator + PATH_UPLOAD;
    }

  public static String getDbDownloadPath(String dsId, String dbName) {
    return ClientConfiguration.getWorkingPath() + File.separator + dsId + File.separator
        + PATH_DATABASE + File.separator + PATH_DOWNLOAD + File.separator + dbName;
  }

  public static String getDatabase() {
    return ClientConfiguration.getWorkingPath() + File.separator + "ebeim.db";
  }

  public static String getUpDownloadDatabase() {
    return ClientConfiguration.getWorkingPath() + File.separator + "updownload.db";
  }
  
  public static String getUpDownloadFailedDatabase() {
    return ClientConfiguration.getWorkingPath() + File.separator + "updownloadfailed.db";
  }
  
  public static String getNetWorkMonitor(){
    return ClientConfiguration.getWorkingPath() + File.separator + "networkmonitor.db";
  }

  public static String getDataSourcePath(String dsId) {
    return ClientConfiguration.getWorkingPath() + File.separator + dsId;
  }

  public static void choosePath(String path, String propertyKey, String propertyValue) {
    if (path == null) {
      path = "";
    }
    File work = new File(path);
    File maxFile = null;
    if (!work.exists()) {
      File[] roots = File.listRoots();
      maxFile = roots[0];
      long max = 0;
      for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        long space = file.getFreeSpace();
        if (space > max) {
          max = space;
          maxFile = file;
        }
      }
      try {
        Log.info("propertyKey:"+propertyKey+"-------propertyValue:"+maxFile.getAbsolutePath() + propertyValue);
        PropertiesUtil.writeProperties(propertyKey, maxFile.getAbsolutePath() + propertyValue);
        ClientConfiguration.setProperties(PropertiesUtil.readAll());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public static String getUploadDataFilePath(String ds, String info_path) {
    if (info_path == null || info_path.length() == 0) {
      return getDataSourcePath(ds) + File.separator + PATH_UPLOAD;
    } else {
      if (info_path.indexOf("\\") == 0) {
        return getDataSourcePath(ds) + File.separator + PATH_UPLOAD + info_path;
      } else {
        return getDataSourcePath(ds) + File.separator + PATH_UPLOAD + File.separator + info_path;
      }

    }
  }

  public static String getZipVersionFilePath(String ds, String version) {
    return getDataSourcePath(ds) + File.separator + PATH_ZIP + File.separator + version;
  }

  public static String getVersionFilePath(String ds, String version) {
    return getDataSourcePath(ds) + File.separator + PATH_VERSION + File.separator + version;
  }

  public static String getDownloadDataFilePath(String ds) {
    return getDataSourcePath(ds) + File.separator + PATH_DOWNLOAD;
  }

  public static void initWorkingPath(String ds) {
    String dsPath = FileUtil.getDataSourcePath(ds);
    createDirectory(dsPath);
    createDirectory(dsPath + File.separator + PATH_VERSION);
    createDirectory(dsPath + File.separator + PATH_ZIP);
    createDirectory(dsPath + File.separator + PATH_UPLOAD);
    createDirectory(dsPath + File.separator + PATH_DOWNLOAD);
  }

  public static void createDirectory(String path) {
    File file = new File(path);
    if (!file.exists()) {
      file.mkdirs();
    }
  }

  public static String getFileId(String customerId, String dsId, String path) {
    String id = customerId + "-" + dsId + "-" + path;
    return DigestUtils.md5Hex(id);
  }

  public static String getFileHd5(String path) {
    String md5 = null;
    FileInputStream in;
    try {
      in = new FileInputStream(new File(path));
      md5 = DigestUtils.md5Hex(in);
      in.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return md5;
  }
  
  public static boolean deleteDir(File dir) {
      if (dir.isDirectory()) {
          String[] children = dir.list();
          for (int i=0; i<children.length; i++) {
              boolean success = deleteDir(new File(dir, children[i]));
              if (!success) {
                  return false;
              }
          }
      }
      // 目录此时为空，可以删除
      return dir.delete();
  }

}
